package com.troll.wanandroid

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*

/**
 *
 * @Author:   TangPeng
 * @Time:     3/23/21
 * @Remark:
 */
class CoroutinesActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coroutines)
    }

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
        }


        /**
         * delay() : 挂起函数，不会造成线程阻塞，但是会挂起协程，只能在协程中使用
         *
         * runBlocking<Unit>{} 用来启动顶层主协程的适配器
         */

        /**
         * runBlocking与coroutineScope的区别：
         *
         * runBlocking与coroutineScope会等待其协程体以及所有子协程结束
         *
         * runBlocking：阻塞当前线程来等待：ch
         */
        private fun runBlockingTest() {

            runBlocking {
                launch {
                    println("当前任务 runBlocking")
                }
                coroutineScope {
                    launch {
                        println("当前任务 from coroutineScope  launch ")
                    }
                    println("当前任务 from coroutineScope   ")
                }
                println("当前任务 over   ")
            }
        }

        private fun cancelTest() {
            runBlocking {
                val job = launch {

                    repeat(1000) {
                        println("job : I am sleeping $it")
                        delay(500)
                    }
                }

                delay(1000)
                println("主线程等待")
                job.cancel()
                job.join()
                println("主线程 Now quit()")
            }
        }

        /**
         * 协程超时  withTimeout
         * 后面会抛出异常：TimeoutCancellationException，它是CancellationException的子类
         */
        private fun timeout() {
            runBlocking {
                withTimeout(10000) {
                    repeat(1000) {
                        println("超时取消协程 $it")
                        delay(500)
                    }
                }
            }

        }

        /******************************************组合挂起函数****************************************/

        /**
         * 默认顺序调用
         */
        private fun combination() {
            runBlocking {
                val one = doSomethingOne()
                val two = doSomethingTwo()
                println("默认顺序调用${one + two}")
            }

        }


        private fun asyncCombination() {
            runBlocking {
                val one = doSomethingOne()
                val two = doSomethingTwo()
                println("并发调用${one + two}")
            }
        }

        suspend fun doSomethingOne(): Int {
            delay(200)
            return 12
        }

        suspend fun doSomethingTwo(): Int {
            delay(200)
            return 13
        }

        /******************************************协程上下文以及调度器****************************************/
        fun coroutineDispatcherTest() {
            runBlocking {
                launch(Dispatchers.Main) {
                    println("Dispatchers.Main  工作线程${Thread.currentThread().name} ")
                }
            }
        }
    }


}
